use bits::field::{BufferReader, BufferWriter};

use crate::{Elf64Addr, Elf64Off, Elf64Xword};

#[repr(C)]
pub struct Header {
    pub seg_type: SegType,
    pub flags: SegmentFlags,
    pub offset: Elf64Off,
    pub vaddr: Elf64Addr,
    /// 物理寻址，这个字段是为那些可以物理寻址的系统保留的。
    pub paddr: Elf64Addr,
    pub filesz: Elf64Xword,
    pub memsz: Elf64Xword,
    pub align: Elf64Xword,
}

impl_borrow!(Header);

trans! {
    SegType: u32,
    SegmentFlags: u32
}

def_const! {
    SegType {
        NULL: 0,
        LOAD: 1,
        DYNAMIC: 2,
        INTERP: 3,
        NOTE: 4,
        SHLIB: 5,
        PHDR: 6
    }
}

impl BufferReader for SegmentFlags {}
impl BufferWriter for SegmentFlags {}

pub mod flags {
    pub struct X;
    pub struct W;
    pub struct R;

    bits::fields! {
        super::SegmentFlags [data] {
            X [0, rw, bool],
            W [1, rw, bool],
            R [2, rw, bool]
        }
    }
}
